<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="IP地址">
          <el-input v-model="form.ip" placeholder="IP地址" />
        </el-form-item>

        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
        <el-table-column
            prop="id"
            label="ID"
            align="center"
          />
          <el-table-column
            prop="uid"
            label="用户UID"
            align="center"
            width="120"
          />
          <el-table-column
            prop="app_mark"
            label="app标识"
            align="center"
          />
          <el-table-column
            prop="name"
            label="设备名称"
            align="center"
            width="150"
          />
          <el-table-column
            prop="country"
            label="国家"
            align="center"
          />
          <el-table-column
            prop="ip_address"
            label="IP"
            align="center"
            width="120"
          />
          <el-table-column
            prop="device_type"
            label="设备类型"
            align="center"
            width="150"
          />
          <el-table-column
            prop="app_version"
            label="APP版本"
            align="center"
          />
          <el-table-column
            prop="device_id"
            label="设备ID"
            align="center"
            width="150"
          />
          <el-table-column
            prop="phone_type"
            label="设备型号"
            align="center"
            width="150"
          />
          <el-table-column
            prop="device_ua"
            label="UA"
            align="center"
            width="150"
          />
          <el-table-column
            prop="phone_os"
            label="系统"
            align="center"
          />
          <el-table-column
            prop="createtime"
            label="登录时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="device_mac"
            label="mac地址"
            align="center"
          />
          <el-table-column
            prop="device_adid"
            label="ADID"
            align="center"
          />
          <el-table-column
            prop="device_oaid"
            label="OAID"
            align="center"
          />
          <el-table-column
            prop="device_imei"
            label="IMEI"
            align="center"
          />
          <!-- <el-table-column
            prop="app_mark"
            label="app标识"
            align="center"
            width="120"
          /> -->
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel,timestamp, pickerDefaultTime } from "@/utils/utils";
import { userLoginApi } from "@/api/slotsgame/user/user";
const tableData = ref([]);
const loading = ref(false);
const formData = {
  timeData: [],
  uid: "",
  ip: "",
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    uid: form.uid,
    ip: form.ip,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await userLoginApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    name: "设备名称",
    country: "国家",
    ip_address: "IP",
    device_type: "设备类型",
    app_version: "APP版本",
    device_id: "设备ID",
    phone_type: "设备型号",
    device_ua: "UA",
    phone_os: "系统",
    device_mac: "mac地址",
    device_adid: "ADID",
    device_oaid: "OAID",
    device_imei: "IMEI",
    app_mark: "app标识",
    createtime: "登录时间",
  };
  exportToExcel(res.data.results, head, "登录记录");
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await userLoginApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
